package com.cw.jeeyt.service.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.cw.jeeyt.common.Constants;
import com.cw.jeeyt.common.utils.SpringContextHolder;
import com.cw.jeeyt.service.bo.system.SystemDictCacheBo;
import com.cw.jeeyt.service.intf.system.SystemDictService;
import com.cw.lang.cache.Cache;

/**
 * 数据字典工具类
 *
* @author chenw
* @version 1.0.0
 */
public class DictUtils {
    private static Logger logger = LoggerFactory.getLogger(DictUtils.class);

    private static SystemDictService getSystemDictService() {
        SystemDictService systemDictService = SpringContextHolder.getBean(SystemDictService.class);
        return systemDictService;
    }

    private static Cache<String,String> getCache() {
        return SpringContextHolder.getBean(Cache.class);
    }

    public static String getItemName(String itemCode, String itemName, String itemValue) {
        SystemDictCacheBo SystemDictCacheBo = getDict(itemCode, itemName, itemValue);
        if (SystemDictCacheBo == null) {
            return "";
        }
        return SystemDictCacheBo.getItemName();
    }

    public String getItemValue(String itemCode, String itemName, String itemValue) {
        SystemDictCacheBo SystemDictCacheBo = getDict(itemCode, itemName, itemValue);
        if (SystemDictCacheBo == null) {
            return "";
        }
        return SystemDictCacheBo.getItemValue();
    }

    /**
     * 获取数据字典信息
     *
     * @param itemCode 字典编码
     * @param itemName 字典名称
     * @param itemValue 字典值
     * @return
     */
    public static SystemDictCacheBo getDict(String itemCode, String itemName, String itemValue) {

        SystemDictCacheBo dictBo = null;
        if (StringUtils.isEmpty(itemCode) && (StringUtils.isEmpty(itemValue) && StringUtils.isEmpty(itemName))) {
            logger.error("查询数据字典参数有错");
            return dictBo;
        }
        try {
            //先去redis缓存查，查不到再数据库查
            List<SystemDictCacheBo> list = getSystemDictService().queryDictListByCache();
            if (list != null && !list.isEmpty()) {
                //循环获取parentId
                String dictParentId = "";
                if (!StringUtils.isEmpty(itemCode)) {
                    for (SystemDictCacheBo SystemDictCacheBo : list) {
                        String dictItemCode = SystemDictCacheBo.getItemCode();
                        if (itemCode.equals(dictItemCode)) {
                            dictParentId = SystemDictCacheBo.getId();
                            break;
                        }
                    }
                    if (StringUtils.isEmpty(dictParentId)) {
                        return null;
                    }
                }
                for (SystemDictCacheBo SystemDictCacheBo : list) {
                    String parentId = SystemDictCacheBo.getParentId();
                    String dictItemName = SystemDictCacheBo.getItemName();
                    String dictItemValue = SystemDictCacheBo.getItemValue();
                    if (!StringUtils.isEmpty(itemName) && !StringUtils.isEmpty(itemValue)) {
                        if (dictParentId.equals(parentId) && itemName.equals(dictItemName) && itemValue.equals(
                            dictItemValue)) {
                            dictBo = SystemDictCacheBo;
                            break;
                        }
                    } else {
                        if (!StringUtils.isEmpty(itemName)) {
                            if (dictParentId.equals(parentId) && itemName.equals(dictItemName)) {
                                dictBo = SystemDictCacheBo;
                                break;
                            }
                        } else if (!StringUtils.isEmpty(itemValue)) {
                            if (dictParentId.equals(parentId) && itemValue.equals(dictItemValue)) {
                                dictBo = SystemDictCacheBo;
                                break;
                            }
                        }
                    }
                }
            }
            if (dictBo == null) {
                SystemDictCacheBo systemDictCacheBo = null;
                if (!StringUtils.isEmpty(itemName) && !StringUtils.isEmpty(itemValue)) {
                    systemDictCacheBo = getSystemDictService().geSystemDictInfo(itemCode, itemName, itemValue);
                } else {
                    if (!StringUtils.isEmpty(itemName)) {
                        systemDictCacheBo = getSystemDictService().getSystemDictByItemName(itemCode, itemName);
                    } else if (!StringUtils.isEmpty(itemValue)) {
                        systemDictCacheBo = getSystemDictService().getSystemDictByItemValue(itemCode, itemValue);
                    }
                }
                if (systemDictCacheBo != null) {
                    dictBo = systemDictCacheBo;
                }
            }
        } catch (Exception e) {
            logger.error("查询数据字典出错：" + e.getMessage());
        }
        return dictBo;
    }

    /**
     * 获取数据字典信息
     *
     * @param itemCode 字典编码
     * @return
     */
    public static SystemDictCacheBo getDictInfo(String itemCode) {
        SystemDictCacheBo dictBo = null;
        if (StringUtils.isEmpty(itemCode)) {
            logger.error("查询数据字典参数有错");
            return dictBo;
        }
        try {
            //先去redis缓存查，查不到再数据库查
            List<SystemDictCacheBo> dictCaches = JSON.parseArray(getCache().get(Constants.DICT_LIST_CACHE),SystemDictCacheBo.class);
            if (dictCaches != null && !dictCaches.isEmpty()) {
                return dictCaches.stream().filter(input -> itemCode.equals(input.getItemCode())).findFirst().orElse(null);
                // for (SystemDictCacheBo systemDictCacheBo : dictCaches) {
                //     String dictItemCode = systemDictCacheBo.getItemCode();
                //     if (itemCode.equals(dictItemCode)) {
                //         return systemDictCacheBo;
                //     }
                // }
            }
            SystemDictCacheBo systemDictCacheBo = getSystemDictService().getSystemDictByItemCode(itemCode);
            if (systemDictCacheBo != null) {
                dictBo = systemDictCacheBo;
            }
        } catch (Exception e) {
            logger.error("查询数据字典出错：" + e.getMessage());
        }
        return dictBo;
    }

    /**
     * 加载数据字典项
     *
     * @param params
     * @return
     */
    public static List<SystemDictCacheBo> getDataDicItem(Map<String, Object> params) {

        List<SystemDictCacheBo> itemList = new ArrayList<>();
        //数据字典key
        String dictKey = (String) params.get("dictKey");
        //不需要加载的字典项
        String subList = (String) params.get("subList");
        //只需要加载的字典项
        String chooseList = (String) params.get("chooseList");
        SystemDictCacheBo systemDictCacheBo;
        try {
            systemDictCacheBo = getSystemDictService().getSystemDictByItemCode(dictKey);
            if (null == systemDictCacheBo) {
                return null;
            }
            List<SystemDictCacheBo> systemDictList = getSystemDictService().getItemDictListByParentId(systemDictCacheBo.getId());
            if (StringUtils.isNotEmpty(chooseList)) {
                List<SystemDictCacheBo> choose_systemDictList = new ArrayList<>();
                for (SystemDictCacheBo m : systemDictList) {
                    if (chooseList.contains(m.getItemValue())) {
                        choose_systemDictList.add(m);
                    }
                }
                systemDictList = choose_systemDictList;
            }
            if (systemDictList != null && !systemDictList.isEmpty()) {
                for (SystemDictCacheBo m : systemDictList) {
                    if (StringUtils.isNotEmpty(subList)) {
                        if (subList.contains(m.getItemValue())) {
                            continue;
                        }
                    }
                    SystemDictCacheBo itemBo = new SystemDictCacheBo();
                    itemBo.setItemName(m.getItemName());
                    itemBo.setItemValue(m.getItemValue());
                    itemList.add(itemBo);
                }
            }
        } catch (Exception e) {
            // 暂时不做处理
            logger.error("加载数据字典项出错：{}", e.getMessage());
        }
        return itemList;
    }
}
